<template>
    <div class="app-container">
        <div class="filter-container">
            <el-form ref="listQuery" :model="listQuery" inline>
                <el-form-item label="医生姓名" prop="name">
                    <el-input size="small" clearable style="width: 150px;" class="filter-item" placeholder="请输入医生姓名" v-model="listQuery.name">
                    </el-input>
                </el-form-item>
                <el-form-item label="所属医院" prop="hid">
                    <el-select clearable class="filter-item" placeholder="请选择所属医院" v-model="listQuery.hid">
                        <el-option v-for="(item, index) in hospitalOptions" :key="item.name" :label="item.name" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="所属科室" prop="did">
                    <el-select clearable class="filter-item" placeholder="请选择所属科室" v-model="listQuery.did">
                        <el-option v-for="(item, index) in departmentOptions" :key="item.name" :label="item.name" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="手机号码" prop="phone">
                    <el-input size="small" clearable style="width: 150px;" class="filter-item" placeholder="请输入手机号码" v-model="listQuery.phone"  :maxlength="phoneMaxLength"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button size="small" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
                    <el-button size="small" class="filter-item" type="danger" icon="el-icon" @click="resetFilter">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div style="height: 42px;line-height: 32px;">
            <el-button type="success" @click="handleAdd" style="float:right" size="small" icon="el-icon-plus">新增医生</el-button>
        </div>
        <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit highlight-current-row style="width: 100%" stripe>
            <el-table-column align="center" width="60px" type="index" label="序号"></el-table-column>
            <el-table-column align="center" label="医生姓名" prop="name"></el-table-column>
            <el-table-column align="center" label="所属医院" prop="hospitalName"></el-table-column>
            <el-table-column align="center" label="所属科室" prop="departmentName"></el-table-column>
            <el-table-column align="center" label="职称" prop="title"></el-table-column>
            <el-table-column align="center" label="手机号" prop="phone"></el-table-column>
            <el-table-column align="center" label="操作" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button type="primary" size="small" @click="handleEdit(scope.row)" icon="el-icon-edit">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDelete(scope.row)" icon="el-icon-delete">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination-container">
            <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page" :page-sizes="[20,50,100]" :page-size="listQuery.size" layout="total, sizes, prev, pager, next" :total="total">
            </el-pagination>
        </div>

        <manage :doctorId="doctorId" :editOrAdd="editOrAdd" :dialogEdit.sync="dialogEdit" @refreshList="getList"></manage>
    </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import manage from '@/views/hospital/doctor-manage'
  import { fetchHospitalList } from '@/api/hospital'
  import { departmentGetList } from '@/api/department'
  import { doctorGetList, doctorDelete } from '@/api/doctor'

  export default {
    name: 'HospitalDoctor',
    data() {
      return {
        phoneMaxLength: 11,
        tableKey: 0,
        list: [],
        total: null,
        listLoading: false,
        hospitalOptions: [],
        departmentOptions: [],
        listQuery: {
          page: 1,
          size: 20,
          name: '',
          phone: '',
          hid: '',
          did: ''
        },
        editOrAdd: 'add',
        doctorId: '',
        dialogEdit: false
      }
    },
    components: {
      manage
    },
    computed: {
      ...mapGetters([
        'fid'
      ])
    },
    created: function() {
      this.getList()
      this.getHospitals()
    },
    watch: {
      'listQuery.hid'(val) {
        this.listQuery.did = ''
        this.departmentOptions = []
        if (val) {
          this.getDepartmentList()
        }
      }
    },
    methods: {
      getHospitals() {
        fetchHospitalList({ page: 1, size: 500 }).then(response => {
          this.hospitalOptions = response.data.list || []
        })
      },
      getDepartmentList() {
        departmentGetList({ page: 1, size: 500, hid: this.listQuery.hid }).then(response => {
          this.departmentOptions = response.data.list || []
        })
      },
      getList() {
        this.listLoading = true
        var query = {
          page: this.listQuery.page,
          size: this.listQuery.size,
          name: this.listQuery.name,
          hid: this.listQuery.hid,
          phone: this.listQuery.phone,
          did: this.listQuery.did
        }
        doctorGetList(query).then(response => {
          this.list = response.data && response.data.list || []
          this.total = Number.parseInt(response.data.count)
          this.listLoading = false
        }).catch(e => {
          this.listLoading = false
        })
      },
      handleFilter() {
        this.listQuery.page = 1
        this.getList()
      },
      resetFilter() {
        this.$refs['listQuery'].resetFields()
      },
      handleSizeChange(val) {
        this.listQuery.size = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.getList()
      },
      handleAdd() {
        this.doctorId = ''
        this.editOrAdd = 'add'
        this.dialogEdit = true
      },
      handleEdit(row) {
        this.doctorId = row.id
        this.editOrAdd = 'edit'
        this.dialogEdit = true
      },
      handleDelete(row) {
        this.$confirm('确定删除' + row.name + '?', '删除提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          doctorDelete({ id: row.id }).then(response => {
            if (response) {
              this.$message({ message: '删除成功', type: 'success', showClose: true })
              this.getList()
            }
          })
        })
      }
    }
  }
</script>

<style scoped></style>
